package com.example.byNight;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class Visualizza extends Activity {
	Button backVisualizza;
	TextView visualizzaRis, appoggio;
	ScrollView visualizzaRis2;
	String[] finalmente;
	ImageView img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.visualizza);
		Intent intent = getIntent();
		String risultati = intent.getStringExtra("ris");
		System.out.println("risultati " + risultati);

		finalmente = risultati.split(";");
		System.out.println("gli elementi sono: " + finalmente.length);
		int num_el = finalmente.length;

		img = (ImageView) findViewById(R.id.immagine);
		try {
			String returned = getInternetData();
			appoggio.setText(returned);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (num_el == 10) {

			Bitmap imgPresa = loadBitmap(finalmente[8]);
			img.setImageBitmap(imgPresa);
			String app = "Descrizione:\n" + finalmente[1] + "\n"
					+ "Contatti: \n" + finalmente[2] + "\n" + finalmente[3]
					+ "\n" + finalmente[5] + "\n Numero posti: "
					+ finalmente[4];
			visualizzaRis = (TextView) findViewById(R.id.visualizza3);
			visualizzaRis.setText(app);

		} else {

			for (int i = 0; i < num_el; i++) {
				System.out.println("locale Elemento " + i + " ="
						+ finalmente[i]);
			}

			Bitmap imgPresa = loadBitmap(finalmente[3]);
			img.setImageBitmap(imgPresa);
			String app = "Evento: " + finalmente[0] + "\nData: "
					+ finalmente[1] + "\nDescrizione:\n" + finalmente[2] + "\n"
					+ "Numero posti: " + finalmente[4] + "\nPresso il locale: "
					+ finalmente[5];
			visualizzaRis = (TextView) findViewById(R.id.visualizza3);
			visualizzaRis.setText(app);

		}

	}

	// metodo reduce da vecchia implementazione
	public String getInternetData() throws Exception {
		BufferedReader in = null;
		String data = null;
		try {
			HttpClient client = new DefaultHttpClient();
			URI website = new URI("http://www.google.it");
			HttpGet request = new HttpGet();
			request.setURI(website);
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			StringBuffer sb = new StringBuffer("");
			String l = "";
			String nl = System.getProperty("line.separator");
			while ((l = in.readLine()) != null) {
				sb.append(l + nl);

			}
			in.close();
			data = sb.toString();
			return data;
		} finally {
			if (in != null) {
				try {
					in.close();
					return data;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static Bitmap loadBitmap(String url) {
		Bitmap bm = null;

		try {

			URL aURL = new URL(url);
			URLConnection conn = aURL.openConnection();
			try {
				conn.connect();
				InputStream is = conn.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				bm = BitmapFactory.decodeStream(bis);
				bis.close();
				is.close();
			} catch (Exception e) {
				System.out.println("IMMAGINE+ " + e.getMessage());
			}

		} catch (IOException e) {
			System.out.println("errore scaricamento file" + e.getMessage());
		}
		return bm;
	}

}
